package cn.rengy.web.framework.sys.userlogin;

import java.util.Date;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rengy.lang.ResultEntity;
import cn.rengy.web.framework.constants.Constants;
import cn.rengy.web.framework.id.IdGenerator;
import cn.rengy.web.framework.property.service.PropertyService;
import cn.rengy.web.framework.sql.DbService;

@Service
public class UserLoginService {
	@Autowired
	private DbService dbService;
	@Autowired
	private PropertyService propertyService;
	@Autowired
	private IdGenerator idGenerator;
	private static Logger logger = LoggerFactory.getLogger(UserLoginService.class);
	
	@Transactional
	public ResultEntity<?> save(Map<String,Object> paramMap){
		String id=MapUtils.getString(paramMap, "id");
		//1.查询用户名重复
		Long count=dbService.querySingleColumn("select_username_unique", paramMap, Long.class);
		if(count>0){
			return ResultEntity.fail("用户名已存在");
		}
		if(StringUtils.isBlank(id)){//新增
			//2.新增登录用户
			Long company_id=null;
			id=idGenerator.generateId().toString();
			paramMap.put("id", id);
			paramMap.put("create_time", new Date());
			paramMap.put("company_id", company_id);
			this.dbService.updateMapParameter("insert_sys_user_login", paramMap);
			
		}else {//修改
			this.dbService.updateMapParameter("update_sys_login_username", paramMap);
		}
		return  ResultEntity.ok();
	}
	
	@Transactional
	public void delete(Map<String,Object> paramMap) {
		Object ids=paramMap.get("ids");
		this.dbService.updates("update_user_login_deleted1",Constants.YES,ids);
		
		
	}
	
	@Transactional
	public void enable(Map<String,Object> paramMap) {
		Object ids=paramMap.get("ids");
		this.dbService.update("update_user_login_disabled0",Constants.NO ,ids);
		
	}
	
	@Transactional
	public void disable(Map<String,Object> paramMap) {
		Object ids=paramMap.get("ids");
		this.dbService.update("update_user_login_disabled0",Constants.Disabled ,ids);
	}
}
